package com.hj.ct.view;

import java.util.ArrayList;
import java.util.List;

import com.hj.ct.R;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class ListPopupWindow extends PopupWindow {
	private ListView listView;
	private List<String> data = new ArrayList<String>();
	private Context mContext;

	public ListPopupWindow(Context context) {
		// TODO Auto-generated constructor stub
		super(context);
		this.mContext = context;
		initView();
		initAdapter();
	}

	private void initView() {
		setAnimationStyle(R.style.popmenu_anim);
		setWindowLayoutMode(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
		setBackgroundDrawable(new ColorDrawable());
		setOutsideTouchable(true);
		setFocusable(true);
		View mContainer = LayoutInflater.from(mContext).inflate(R.layout.list_popup, null);
		listView = (ListView) mContainer.findViewById(R.id.listview);
		setContentView(mContainer);
	}

	private void initAdapter() {
		listView.setAdapter(new MyAdapter());
	}

	public void setData(List<String> data) {
		this.data = data;
		notifyDataChanged();
	}

	public void notifyDataChanged() {
		((MyAdapter) (listView.getAdapter())).notifyDataSetChanged();
	}

	public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
		listView.setOnItemClickListener(mOnItemClickListener);
	}

	public String getItem(int position) {
		return (String) listView.getAdapter().getItem(position);
	}

	class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return data.size();
		}

		@Override
		public String getItem(int position) {
			// TODO Auto-generated method stub
			return data.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder viewHolder;
			if (convertView == null) {
				viewHolder = new ViewHolder();
				convertView = new LinearLayout(mContext);
				// convertView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
				((LinearLayout) convertView).setOrientation(LinearLayout.VERTICAL);
				((LinearLayout) convertView).setGravity(Gravity.CENTER);
				viewHolder.tv = new TextView(mContext);
				viewHolder.tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
				viewHolder.tv.setBackgroundResource(R.drawable.popup_item_bg);
				viewHolder.tv.setGravity(Gravity.CENTER);
				viewHolder.tv.setPadding(16, 16, 16, 16);
				viewHolder.tv.setTextColor(mContext.getResources().getColor(R.color.gray_333333));
				viewHolder.tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
				((LinearLayout) convertView).addView(viewHolder.tv);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			viewHolder.tv.setText(getItem(position));
			return convertView;
		}

	}

	class ViewHolder {
		TextView tv;
	}

}
